﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;


using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace HighDivePrinter.Model
{
    /// <summary>
    /// List of competitions. Used for display of racing history.
    /// Additionaly it is used when you want to start a non-start race (a list will appear of all-started races).
    /// </summary>
    [Serializable()]
    public class ContestList : IContestList
    {
       
        private BindingList<Contest> _listContest;

        #region Properties
        public BindingList<Contest> ListContest
        { 
            get { return _listContest;}
            set { _listContest=value;}
        } 
	    #endregion

        #region Constructors
        public ContestList()
        {
            ListContest = new BindingList<Contest>();
        }

        #endregion

        #region Methods

        #endregion

        #region IContestList Members

        void IContestList.AddContest(Contest c)
        {
            _listContest.Add(c);
        }

        void IContestList.RemoveContest(int index)
        {
           _listContest.RemoveAt(index);
        }

        void IContestList.SaveContest()
        {
            Stream stream = new FileStream("Contests.xml", FileMode.Create, FileAccess.Write, FileShare.Write);
            XmlSerializer xs = new XmlSerializer(typeof(ContestList));
            xs.Serialize(stream, this);
            stream.Close();
        }

        void IContestList.ReadContest()
        {
            Stream stream = new FileStream("Contests.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
            XmlSerializer xs = new XmlSerializer(typeof(ContestList));
            ContestList dt = (ContestList)xs.Deserialize(stream);

            _listContest.Clear();
            foreach (var c in dt.ListContest)
            {
                _listContest.Add(c);
            }
            stream.Close();
        }

        #endregion
    }
   
}
